package com.b511.swpu.cementbackend.controller;

import com.b511.swpu.cementbackend.dto.input.MetaSystemUnitValueInputDto;
import com.b511.swpu.cementbackend.dto.input.SysUserInputDto;
import com.b511.swpu.cementbackend.entity.SysUser;
import com.b511.swpu.cementbackend.service.MetaCategoryService;
import com.b511.swpu.cementbackend.service.MetaSystemUnitService;
import com.b511.swpu.cementbackend.entity.MetaSystemUnit;
import com.b511.swpu.cementbackend.repository.MetaSystemUnitRepository;
import com.b511.swpu.cementbackend.repository.MetaUnitCategoryRepository;
import com.b511.swpu.cementbackend.service.MetaSystemUnitValueService;
import com.b511.swpu.cementbackend.service.SysUserService;
import com.b511.swpu.cementbackend.util.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "单位转换", tags = {"单位转换"})
@RestController
@RequestMapping("/category")
public class MetaUnitCategoryController {

    private final MetaUnitCategoryRepository metaUnitCategoryRepository;

    private final MetaSystemUnitRepository metaSystemUnitRepository;

    private final MetaSystemUnitService metaSystemUnitService;

    private final MetaSystemUnitValueService metaSystemUnitValueService;

    private final MetaCategoryService metaCategoryService;

    public MetaUnitCategoryController(MetaUnitCategoryRepository metaUnitCategoryRepository, MetaSystemUnitRepository metaSystemUnitRepository, MetaSystemUnitService metaSystemUnitService, MetaSystemUnitValueService metaSystemUnitValueService, MetaCategoryService metaCategoryService) {
        this.metaUnitCategoryRepository = metaUnitCategoryRepository;
        this.metaSystemUnitRepository = metaSystemUnitRepository;
        this.metaSystemUnitService = metaSystemUnitService;
        this.metaSystemUnitValueService = metaSystemUnitValueService;
        this.metaCategoryService = metaCategoryService;
    }

    @GetMapping
    public R getCategory() {
        return R.ok().putData(metaUnitCategoryRepository.findAll());
    }

    @GetMapping("/system/unit")
    public R getSystemUnit() {
        return R.ok().putData(metaSystemUnitRepository.findAll());
    }

    @GetMapping("/system/unit/{id}")
    public R getSystemUnitById(@PathVariable Long id) {
        return metaSystemUnitValueService.getMetaSystemUnitValueById(id);
    }

    @PostMapping("/system/unit")
    public R addSystemUnit(@RequestBody MetaSystemUnit metaSystemUnit) {
        return metaSystemUnitService.addSystemUnit(metaSystemUnit);
    }

    @PutMapping("/system/unit")
    public R updateMetaSystemUnitValue(@RequestBody List<MetaSystemUnitValueInputDto> list) {
        return metaSystemUnitValueService.updateMetaSystemUnitValue(list);
    }

    @PutMapping("/user/{metaSystemUnitId}")
    @Transactional
    public R updateUserUnit(@PathVariable("metaSystemUnitId") Long metaSystemUnitId) {
        return metaCategoryService.updateUserUnit(metaSystemUnitId);
    }
}
